package com.bw.a1908.core.db;

import com.blankj.utilcode.util.Utils;

/**
 * 数据库管理类
 * 1.对外提供Session CRUD
 * 2.对内Master 数据库版本管理
 */
public class DBManager {

    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            MyOpenHelper devOpenHelper = new MyOpenHelper(Utils.getApp(), "Data.db");
            daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        }
        return daoMaster;
    }

    /**
     * CRUD
     */
    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public void close(){
        if (daoSession != null) {
            daoSession.clear();
            daoSession = null;
        }
        if (daoMaster != null) {
            daoMaster = null;
        }
    }

    private static DBManager dbManager;

    public static DBManager getInstance() {
        if (dbManager == null) {
            dbManager = new DBManager();
        }
        return dbManager;
    }

    private DBManager() {

    }

}
